package com.yzt.minemodule.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yzt.zhuju.base.BaseActivity;
import com.yzt.zhuju.constants.IntentConstant;
import com.yzt.zhuju.event.RxBus;
import com.yzt.zhuju.event.user.EditUserInfoEvent;
import com.yzt.zhuju.event.user.PersonalRefresh;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.widget.tuiview.TUIFrameLayout;
import com.yzt.minemodule.R;
import com.yzt.plugin_network.response.HttpResponse;

/**
 * author : bob
 * date   : 2022/12/8
 * desc   : 编辑个人信息
 */
@SuppressLint("SetTextI18n")
public class EditInfoActivity extends BaseActivity {

    /**
     * 编辑昵称
     */
    public static final int TYPE_NICKNAME = 0;

    /**
     * 编辑签名
     */
    public static final int TYPE_AUTOGRAPH = 1;

    /**
     * 编辑职业
     */
    public static final int TYPE_JOB = 2;

    private int type;

    private String editInfo;

    private EditText desc;
    private TextView tv_count;

    public static void start(Activity activity, int type, String content, int resultCode) {
        start(activity, type, "", content, resultCode);
    }

    public static void start(Activity activity, int type, String id, String content, int resultCode) {
        Intent intent = new Intent(activity, EditInfoActivity.class);
        intent.putExtra(IntentConstant.INTENT_TYPE, type);
        intent.putExtra(IntentConstant.INTENT_ID, id);
        intent.putExtra(IntentConstant.INTENT_CONTENT, content);
        activity.startActivityForResult(intent, resultCode);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_edit_info;
    }

    @Override
    public void installViews() {
        setFullScreen(false);
        TUIFrameLayout descFrameLayout = findViewById(R.id.edit_desc_layout);
        TUIFrameLayout nameFrameLayout = findViewById(R.id.edit_name_layout);

        desc = findViewById(R.id.edit_desc_info);
        EditText name = findViewById(R.id.edit_name_info);
        tv_count = findViewById(R.id.tv_count);

        TextView nameDesc = findViewById(R.id.tv_name_desc);

        type = getIntent().getIntExtra(IntentConstant.INTENT_TYPE, 0);
        String content = getIntent().getStringExtra(IntentConstant.INTENT_CONTENT);

        switch (type) {
            case TYPE_NICKNAME:
                mTitleBar.setTitle("修改昵称");
                name.setText(content);
                descFrameLayout.setVisibility(View.GONE);
                nameFrameLayout.setVisibility(View.VISIBLE);
                nameDesc.setVisibility(View.VISIBLE);
                break;
            case TYPE_AUTOGRAPH:
                mTitleBar.setTitle("个人简介");
                desc.setText(content);
                descFrameLayout.setVisibility(View.VISIBLE);
                nameFrameLayout.setVisibility(View.GONE);
                nameDesc.setVisibility(View.GONE);
                break;

            case TYPE_JOB:
                mTitleBar.setTitle("修改职业");
                name.setText(content);
                descFrameLayout.setVisibility(View.GONE);
                nameFrameLayout.setVisibility(View.VISIBLE);
                nameDesc.setVisibility(View.GONE);
                break;
            default:
                break;
        }

        desc.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String descContent = desc.getText().toString();
                tv_count.setText(descContent.length() + "/300");
            }
        });

        findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                switch (type) {
                    case TYPE_NICKNAME:
                        EditText name = findViewById(R.id.edit_name_info);
                        editInfo = name.getText().toString();
                        BizLayer.getInstance().getAccountModule().editUserInfo("nickName", editInfo, new AresSubscriber<Object>() {
                            @Override
                            protected void onSuccess(Object response) {
                                Intent intent = new Intent();
                                intent.putExtra(IntentConstant.INTENT_CONTENT, editInfo);
                                setResult(RESULT_OK, intent);
                                finish();
                                RxBus.getDefault().post(new EditUserInfoEvent());
                            }

                            @Override
                            protected void onError(int code, String info, HttpResponse<Object> response) {
                                super.onError(code, info, response);
                            }

                            @Override
                            protected void onError(int code, String info) {
                                super.onError(code, info);
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                            }
                        });
                        break;
                    case TYPE_AUTOGRAPH:
                        editInfo = desc.getText().toString();
                        BizLayer.getInstance().getAccountModule().editUserInfo("intro", editInfo, new AresSubscriber<Object>() {
                            @Override
                            protected void onSuccess(Object response) {
                                Intent intent = new Intent();
                                intent.putExtra(IntentConstant.INTENT_CONTENT, editInfo);
                                setResult(RESULT_OK, intent);
                                finish();
                                RxBus.getDefault().post(new EditUserInfoEvent());
                            }
                        });
                        break;
                    case TYPE_JOB:
                        EditText job = findViewById(R.id.edit_name_info);
                        editInfo = job.getText().toString();
                        BizLayer.getInstance().getAccountModule().editUserInfo("job", editInfo, new AresSubscriber<Object>() {
                            @Override
                            protected void onSuccess(Object response) {
                                Intent intent = new Intent();
                                intent.putExtra(IntentConstant.INTENT_CONTENT, editInfo);
                                setResult(RESULT_OK, intent);
                                finish();
                                RxBus.getDefault().post(new EditUserInfoEvent());
                            }

                            @Override
                            protected void onError(int code, String info, HttpResponse<Object> response) {
                                super.onError(code, info, response);
                            }

                            @Override
                            protected void onError(int code, String info) {
                                super.onError(code, info);
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                            }
                        });
                        break;
                    default:
                        break;
                }
                RxBus.getDefault().post(new PersonalRefresh());
            }
        });
    }
}

